/*
 * @Description: 
 * @Author: 赵大壮
 * @Date: 2022-07-04 09:50:51
 * @LastEditTime: 2024-07-23 16:27:19
 * @LastEditors: 赵大壮
 */
import nyNetwork from "@/api/nyBaseNetwork";
import DZModelPage from "@/model/DZModelPage";
import uploadFile from "@/utils/DZUploadFile";
import nyMessageToast from "@/utils/NYMessageToast";
import DZModelReportLog from "@/views/报告管理/model/DZModelReportLog";
import DZModelInvInfo from "../model/DZModelInvInfo";
import DZModelInvlnReq from "../model/DZModelInvlnReq";
import DZModelQuesiton from "../model/DZModelQuesiton";
import DZModelTemplate from "../model/DZModelTemplate";

class DZServiceInves {

    dialog_edit_base = false;

    str_key = "";

    m_page = new DZModelPage(10);

    list_inv: Array<DZModelInvInfo> = [];

    m_inv = new DZModelInvInfo();

    list_inv_with_assess: Array<DZModelInvInfo> = [];

    list_base_info: Array<DZModelQuesiton> = [];

    m_base_info = new DZModelQuesiton();

    list_inv_question: Array<DZModelQuesiton> = [];

    m_inv_question = new DZModelQuesiton();

    m_inv_question_edit = new DZModelQuesiton();

    list_template_base: Array<DZModelTemplate> = [];

    m_template_base = new DZModelTemplate();

    list_template_question: Array<DZModelTemplate> = [];

    m_template_question = new DZModelTemplate();

    template_id = "";

    list_req: Array<DZModelInvlnReq> = [];

    list_log: Array<DZModelReportLog> = [];

    m_req = new DZModelInvlnReq();

    list_inv_id = [];

    //=============基本信息管理===============
    net_list_base() {
        var arr_data = [];
        nyNetwork.net("基本信息列表", {}, data => {
            for (const item of data) {
                var baseInfo = new DZModelQuesiton();
                baseInfo.id = item.iId ?? '';
                baseInfo.name = item.sName ?? '';
                baseInfo.inputType = parseInt(item.iInputType) ?? 0;
                baseInfo.selectContent = item.sSelectContent ?? '';
                if (item.sSelectContent) {
                    baseInfo.contentList = item.sSelectContent.split(",");
                }
                arr_data.push(baseInfo);
            }
            this.list_base_info = arr_data;
        })
    }

    net_add_base(success: () => void) {
        nyNetwork.net("添加基本信息", { name: this.m_base_info.name, inputType: this.m_base_info.inputType, selectContent: this.m_base_info.selectContent }, data => {
            this.m_base_info.init();
            success();
        })
    }

    net_del_base(success: () => void) {
        nyNetwork.net("删除基本信息", { baseId: this.m_base_info.id }, data => {
            this.m_base_info.init();
            success();
        })
    }

    net_list_template_base() {
        var arr_data = [];
        nyNetwork.net("基本信息模板分页列表", { ...this.m_page, name: this.m_template_base.name }, data => {
            this.m_page.total = data.total;
            for (const item of data.list) {
                var template = new DZModelTemplate();
                template.id = item.iId ?? '';
                template.name = item.sName ?? '';
                template.questionList = item.sQuestionList ?? '';
                template.list_question = template.questionList.split(",") ?? [];
                arr_data.push(template);
            }
            this.list_template_base = arr_data;
        })
    }

    net_list_template_base_all() {
        var arr_data = [];
        nyNetwork.net("基本信息模板列表", {}, data => {
            for (const item of data) {
                var template = new DZModelTemplate();
                template.id = item.iId ?? '';
                template.name = item.sName ?? '';
                template.questionList = item.sQuestionList ?? '';
                template.list_question = template.questionList.split(",") ?? [];
                arr_data.push(template);
            }
            this.list_template_base = arr_data;
        })
    }

    net_add_template_base(success: () => void) {
        nyNetwork.net("添加基本信息模板", { name: this.m_template_base.name, questionList: this.m_template_base.questionList }, data => {
            this.m_template_base.init();
            success();
        })
    }

    net_del_template_base(success: () => void) {
        nyNetwork.net("删除基本信息模板", { id: this.m_template_base.id }, data => {
            this.m_template_base.init();
            success();
        })
    }

    net_info_template_base(templateId) {
        var arr_data = [];
        nyNetwork.net("基本信息模板详情", { templateId: templateId }, data => {
            for (const item of data) {
                var baseInfo = new DZModelQuesiton();
                baseInfo.id = item.iId ?? '';
                baseInfo.name = item.sName ?? '';
                baseInfo.inputType = parseInt(item.iInputType) ?? 0;
                baseInfo.selectContent = item.sSelectContent ?? '';
                if (item.sSelectContent) {
                    baseInfo.contentList = item.sSelectContent.split(",");
                }
                arr_data.push(baseInfo);
            }
            this.list_base_info = arr_data;
        })
    }

    //=============问卷问题管理===============
    net_list_question() {
        var arr_data = [];
        nyNetwork.net("问卷问题列表", { inv: this.m_req.id, name: this.str_key }, data => {
            for (const item of data) {
                var baseInfo = new DZModelQuesiton();
                baseInfo.id = item.iId ?? '';
                baseInfo.name = item.sName ?? '';
                baseInfo.inputType = parseInt(item.iInputType) ?? 0;
                baseInfo.selectContent = item.sSelectContent ?? '';
                if (item.sSelectContent) {
                    baseInfo.contentList = item.sSelectContent.split(",");
                }
                arr_data.push(baseInfo);
            }
            this.list_inv_question = arr_data;
        })
    }

    net_add_question(success: () => void) {
        nyNetwork.net("添加问卷问题", { name: this.m_inv_question.name, inputType: this.m_inv_question.inputType, selectContent: this.m_inv_question.selectContent }, data => {
            this.m_inv_question.init();
            success();
        })
    }
    net_base_inuse(data) {
        nyNetwork.net("问题是否被模板使用", { id: data.id }, res => {
            if (res > 0) {
                nyMessageToast.error("该问题已被模板使用，不能编辑")
            } else {
                this.m_inv_question_edit = Object.assign({}, data)
                this.dialog_edit_base = true
            }
        })
    }
    net_edit_question(data, success: () => void) {
        nyNetwork.net("修改问卷问题", {
            id: data.id,
            name: data.name,
            inputType: data.inputType,
            selectContent: data.selectContent
        }, data => {
            this.m_inv_question.init();
            success();
        })
    }
    net_del_question(success: () => void) {
        nyNetwork.net("删除问卷问题", { questionId: this.m_inv_question.id }, data => {
            this.m_inv_question.init();
            success();
        })
    }
    net_list_template_question() {
        var arr_data = [];
        nyNetwork.net("问卷问题模板分页列表", { ...this.m_page, name: this.m_template_question.name }, data => {
            this.m_page.total = data.total;
            for (const item of data.list) {
                var template = new DZModelTemplate();
                template.id = item.iId ?? '';
                template.name = item.sName ?? '';
                template.questionList = item.sQuestionList ?? '';
                template.list_question = template.questionList.split(",") ?? [];
                arr_data.push(template);
            }
            this.list_template_question = arr_data;
        })
    }


    net_list_template_question_all() {
        var arr_data = [];
        nyNetwork.net("问卷问题模板列表", {}, data => {
            for (const item of data) {
                var template = new DZModelTemplate();
                template.id = item.iId ?? '';
                template.name = item.sName ?? '';
                template.questionList = item.sQuestionList ?? '';
                template.list_question = template.questionList.split(",") ?? [];
                arr_data.push(template);
            }
            this.list_template_question = arr_data;
        })
    }

    net_add_template_question(success: () => void) {
        nyNetwork.net("添加问卷问题模板", { name: this.m_template_question.name, questionList: this.m_template_question.questionList }, data => {
            this.m_template_question.init();
            success();
        })
    }

    net_del_template_question(success: () => void) {
        nyNetwork.net("删除问卷问题模板", { id: this.m_template_question.id }, data => {
            this.m_template_question.init();
            success();
        })
    }

    net_edit_template_question_name(id, name, success: () => void) {
        nyNetwork.net("修改问卷问题模板名称", { id: id, name: name }, data => {
            this.m_template_question.init();
            success();
        })
    }

    net_info_template_question(templateId) {
        var arr_data = [];
        nyNetwork.net("问卷问题模板详情", { templateId: templateId }, data => {
            for (const item of data) {
                var baseInfo = new DZModelQuesiton();
                baseInfo.id = item.iId ?? '';
                baseInfo.name = item.sName ?? '';
                baseInfo.inputType = parseInt(item.iInputType) ?? 0;
                baseInfo.selectContent = item.sSelectContent ?? '';
                if (item.sSelectContent) {
                    baseInfo.contentList = item.sSelectContent.split(",");
                }
                arr_data.push(baseInfo);
            }
            this.list_inv_question = arr_data;
        })
    }

    net_list_question_move(str,id,success: () => void){
        nyNetwork.net("问卷问题排序",{str:str,id:id},data =>{
            success()
        })
    }


    net_list_inv() {
        var arr_data = [];
        nyNetwork.net("调查问卷列表", { ...this.m_page, name: this.m_inv.name }, data => {
            this.m_page.total = data.total;
            for (const item of data.list) {
                var inv = new DZModelInvInfo();
                inv.id = item.iId ?? '';
                inv.name = item.sName ?? '';
                inv.listCover = item.iListCover ?? '';
                if (inv.listCover) {
                    inv.listCoverUrl = nyNetwork.image(inv.listCover);
                }
                inv.infoCover = item.iInfoCover ?? '';
                if (inv.infoCover) {
                    inv.infoCoverUrl = nyNetwork.image(inv.infoCover);
                }
                inv.type = item.iType ?? 0;
                inv.baseTemplate = item.iBaseTemplate ?? '';
                inv.baseTemplateName = item.sBaseTemplateName ?? ''
                inv.questionTemplate = item.iQuestionTemplate ?? '';
                inv.questionTemplateName = item.sQuestionTemplateName ?? '';
                inv.assessId = item.iAssessId ?? '';
                inv.assessName = item.sAssessName ?? '';
                inv.erCordUrl = item.erCordUrl ?? '';
                inv.sort = item.iSort ?? 0;
                arr_data.push(inv);
            }
            this.list_inv = arr_data;
        })
    }

    net_list_inv_all() {
        var arr_data = [];
        nyNetwork.net("调查问卷全部列表", {}, data => {
            for (const item of data) {
                var inv = new DZModelInvInfo();
                inv.id = item.iId ?? '';
                inv.name = item.sName ?? '';
                arr_data.push(inv);
            }
            this.list_inv = arr_data;
        })
    }

    net_list_inv_all_without_assess() {
        var arr_data = [];
        nyNetwork.net("调查问卷全部列表", { type: 0 }, data => {
            for (const item of data) {
                var inv = new DZModelInvInfo();
                inv.id = item.iId ?? '';
                inv.name = item.sName ?? '';
                arr_data.push(inv);
            }
            this.list_inv = arr_data;
        })
    }

    net_list_inv_all_with_assess() {
        var arr_data = [];
        nyNetwork.net("调查问卷全部列表", { type: 1 }, data => {
            for (const item of data) {
                var inv = new DZModelInvInfo();
                inv.id = item.iId ?? '';
                inv.name = item.sName ?? '';
                arr_data.push(inv);
            }
            this.list_inv_with_assess = arr_data;
        })
    }

    net_add_inv(success: () => void) {
        if (this.m_inv.name.nyIsEmpty()) {
            nyMessageToast.error("请输入问卷名称")
            return;
        }

        if (this.m_inv.listCover.nyIsEmpty()) {
            nyMessageToast.error("请上传列表封面")
            return;
        }
        if (this.m_inv.infoCover.nyIsEmpty()) {
            nyMessageToast.error("请上传详情封面")
            return;
        }
        if (!this.m_inv.questionTemplate) {
            nyMessageToast.error("请选择问卷问题摸板")
            return;
        }
        if (this.m_inv.type == 1) {
            if (!this.m_inv.baseTemplate) {
                nyMessageToast.error("请选择基础信息摸板")
                return;
            }
            if (!this.m_inv.assessId) {
                nyMessageToast.error("请关联量表")
                return;
            }

        }
        nyNetwork.net("添加调查问卷", { ...this.m_inv }, data => {
            this.m_inv.init();
            success();
        })
    }

    net_edit_inv(success: () => void) {
        nyNetwork.net("修改调查问卷", { ...this.m_inv }, data => {
            this.m_inv.init();
            success();
        })
    }

    net_del_inv(success: () => void) {
        nyNetwork.net("删除调查问卷", { id: this.m_inv.id }, data => {
            this.m_inv.init();
            success();
        })
    }

    net_info_inv() {
        nyNetwork.net("调查问卷详情", { id: this.m_inv.id }, data => {
            this.m_inv.parse(data);
        })
    }


    net_upload_image(file, success: (data) => void, filed: () => void) {
        uploadFile.uploadImage(file,
            (res) => {
            }, (res) => {
                nyMessageToast.success("图片上传成功")
                success(res + '');
            }, (res) => {
                nyMessageToast.error("图片上传失败")
                filed();
            });
    }

    net_list_export_inv() {
        var arr_data = [];
        nyNetwork.net("调查问卷结果", { companyId: this.m_req.companyId, ivnesId: this.m_req.id, date: (this.m_req.date ? this.m_req.date / 1000 : null), ...this.m_page }, data => {
            this.m_page.total = data.total;
            for (const item of data.list) {
                var req = new DZModelReportLog();
                req.name = item.sfileName ?? '';
                req.path = item.sfileUrl ?? '';
                req.time = item.createTime ?? '';
                arr_data.push(req);
            }
            this.list_log = arr_data;
        })
    }
    net_list_invz() {
        this.list_inv_id = [];
        var arr_data = [];
        nyNetwork.net("综合问卷记录", { companyId: this.m_req.companyId, ivnesId: this.m_req.id, date: this.m_req.date, ...this.m_page }, data => {
            this.m_page.total = data.total;
            for (const item of data.list) {
                var req = new DZModelInvlnReq();
                req.company = item.companyName ?? '';
                req.departmentName = item.departmentName ?? '';
                req.job = item.jobNum ?? '';
                req.phone = item.userPhone ?? '';
                req.name = item.userName ?? '';
                req.invsName = item.invsName ?? '';
                req.assessNmae = item.assessName ?? '';
                req.time = item.createTime ?? '';
                this.list_inv_id.push(item.iId)
                arr_data.push(req);
            }
            this.list_req = arr_data;
        })
    }

    net_export_inves() {
        nyNetwork.download("调查结果导出", "?idList=" + (this.list_inv_id.join(",")));
    }

    net_down_load(path) {
        nyNetwork.download("下载文件", "?path=" + encodeURIComponent(path));
    }

    net_export_invez() {
        nyNetwork.down("综合问卷记录导出", { companyId: this.m_req.companyId, ivnesId: this.m_req.id, date: this.m_req.date }, '综合问卷记录')
    }
}
export default DZServiceInves;